package com.surelution.tinyerp

class CostListController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [costListInstanceList: CostList.list(params), costListInstanceTotal: CostList.count()]
    }

    def create = {
        def costListInstance = new CostList()
        costListInstance.properties = params
        return [costListInstance: costListInstance]
    }

    def save = {
        def costListInstance = new CostList(params)
        if (costListInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'costList.label', default: 'CostList'), costListInstance.id])}"
            redirect(action: "show", id: costListInstance.id)
        }
        else {
            render(view: "create", model: [costListInstance: costListInstance])
        }
    }

    def show = {
        def costListInstance = CostList.get(params.id)
        if (!costListInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'costList.label', default: 'CostList'), params.id])}"
            redirect(action: "list")
        }
        else {
            [costListInstance: costListInstance]
        }
    }

    def edit = {
        def costListInstance = CostList.get(params.id)
        if (!costListInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'costList.label', default: 'CostList'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [costListInstance: costListInstance]
        }
    }

    def update = {
        def costListInstance = CostList.get(params.id)
        if (costListInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (costListInstance.version > version) {
                    
                    costListInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'costList.label', default: 'CostList')] as Object[], "Another user has updated this CostList while you were editing")
                    render(view: "edit", model: [costListInstance: costListInstance])
                    return
                }
            }
            costListInstance.properties = params
            if (!costListInstance.hasErrors() && costListInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'costList.label', default: 'CostList'), costListInstance.id])}"
                redirect(action: "show", id: costListInstance.id)
            }
            else {
                render(view: "edit", model: [costListInstance: costListInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'costList.label', default: 'CostList'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def costListInstance = CostList.get(params.id)
        if (costListInstance) {
            try {
                costListInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'costList.label', default: 'CostList'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'costList.label', default: 'CostList'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'costList.label', default: 'CostList'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def costAnalysis = {
		def result
		def startDay = new Date()
		def endDay = new Date()
		if (params.startDay&& params.endDay){
			startDay = params.startDay
			endDay = params.endDay
			result = costSearch(startDay,endDay)
			if (result==0){
				flash.message = "${message(code: 'default.nofound.message')}"
				redirect(action:"feeTotal")
			}
		}
		[startDay:startDay,endDay:endDay,result:result]
	}
	
	def costSearch(startDay,endDay){
		println "${startDay},${endDay}"
		def syb = new StringBuilder("select cl.pOrder,cl.directMaterial,cl.manual,cl.produce,cl.process,")
		syb.append '''
				cl.produce,
				cl.produce,
				cl.produce,
				cl.produce,
				cl.produce 
				from CostList cl where cl.pOrder.assignTime <= :endDay and cl.pOrder.assignTime >= :startDay'''
		def result = CostList.executeQuery(syb.toString(),['startDay':startDay, 'endDay':endDay])
		println result
		def costAmount = new HashMap()
		result.each {
			costAmount.put(it[1], it[3]/1000)
			it[9] = it[1] + it[2] + it[3] + it[4]
			it[5] = it[1]/it[9]*100
			it[6] = it[2]/it[9]*100
			it[7] = it[3]/it[9]*100
			it[8] = it[4]/it[9]*100
		}
		println result
		return result
	}
}
